package com.project8.main;

import java.util.ArrayList;
import com.project8.book.Chapter;
import com.project8.book.Figure;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ToFAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<Figure> mThumbIds;
    private TableOfFigure mainInstance;
    private Chapter thisChapter;
        
    public ToFAdapter(Context c, ArrayList<Figure> allThumbnails, TableOfFigure main, Chapter chapterOnDisplay) 
    {
        mContext = c;
        mThumbIds = allThumbnails;
        mainInstance = main;   
        thisChapter = chapterOnDisplay;
    }

    public int getCount() {
        return mThumbIds.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) 
    {
    	if (convertView == null) {
    	LinearLayout pageView;
        TextView label = new TextView(mContext);
        ImageView theFigure = new ImageView(mContext);
        Drawable d = Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/Textbooks/" + mainInstance.book.getBookTitle() + "/res/" + mThumbIds.get(position).getFigureName());
                
        
        
          // if it's not recycled, initialize some attributes
            pageView = new LinearLayout(mContext);
            pageView.setLayoutParams(new GridView.LayoutParams(100, 135));
     		pageView.setPadding(12, 12, 12, 12);
        
         
         pageView.setOrientation(LinearLayout.VERTICAL);
         label.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
         theFigure.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
         
         label.setText("Figure " + thisChapter.getId() + "." + thisChapter.getAllFigures().get(position).getFigureNumber());
         theFigure.setImageDrawable(d);
                                  
         label.setGravity(Gravity.CENTER_HORIZONTAL);
         label.setTextColor(Color.parseColor("#FF000000"));
         
         pageView.addView(label);
         pageView.addView(theFigure);

         final int pos2 = position;
         final Drawable d2 = d;
                  	    
         OnTouchListener listener = new OnTouchListener()
 	    {
 	    	ImageView popOut = (ImageView)mainInstance.findViewById(R.id.popOutImage);	    //Displays pop-up thumbnail
 	    	LinearLayout popOutBorder = (LinearLayout)mainInstance.findViewById(R.id.popOutImageBorder);
 	    	TextView popOutCaption = (TextView)mainInstance.findViewById(R.id.popOutImageCaption);
 	    	 	    	
 	    	long lastTouchTime = -1;
 	    	View lastTouched;
 	    		    	
 			public boolean onTouch(View arg0, MotionEvent arg1) 
 			{ 	    		
 				if(arg1.getAction() == MotionEvent.ACTION_DOWN)
 				{
 				    long thisTime = System.currentTimeMillis();
 			
 				    System.out.println(thisChapter.getAllFigures().get(pos2).getFigurePage());
 				    
 				    if ((thisTime - lastTouchTime < 800) && (arg0.equals(lastTouched)))			//Double Tap
				    {
 				    	String anchor = thisChapter.getAllFigures().get(pos2).getFigurePage().getPageContent() + "#" + thisChapter.getAllFigures().get(pos2).getFigureName();
 				    	
 				    	mainInstance.returnResult(thisChapter.getAllFigures().get(pos2).getFigurePage(), anchor);
 				    	
 				    	//mainInstance.bookContent.loadUrl(thisChapter.getAllFigures().get(pos2).getFigurePage().getPageContent() + "#" + thisChapter.getAllFigures().get(pos2).getFigureName());
 				    }
 				    else 
 				    {
 				        // too slow
 				        lastTouchTime = thisTime;
 				         				        
 				        popOut.setImageDrawable(d2);
 						popOut.setVisibility(View.VISIBLE);
 						popOutBorder.setVisibility(View.VISIBLE);
 						 
 						String figNum = "Figure " + thisChapter.getId() + "." + thisChapter.getAllFigures().get(pos2).getFigureNumber();
 						popOutCaption.setText(figNum + ". " + thisChapter.getAllFigures().get(pos2).getFigureCaption());
						popOutCaption.setVisibility(View.VISIBLE);
 				    }
 					
 				   lastTouched = arg0;
 				    
 					return true;
 				}

 				if(arg1.getAction() == MotionEvent.ACTION_UP)
 				{
 					popOut.setVisibility(View.GONE);
 					popOutBorder.setVisibility(View.GONE);
 					popOutCaption.setVisibility(View.GONE);
 										
 					return true;
 				}
 				if(arg1.getAction() == MotionEvent.ACTION_CANCEL)
 				{
 					popOut.setVisibility(View.GONE);
 					popOutBorder.setVisibility(View.GONE);
 					popOutCaption.setVisibility(View.GONE);
 					 					
 					return true;
 				}
 				
 				return true;	
 			}	
 	    };
         
 	    pageView.setOnTouchListener(listener);
 	    label.setOnTouchListener(listener);
 	    theFigure.setOnTouchListener(listener);
         
        return pageView;
    	}
    	else
    	{
    		return convertView;
    	}
     }
}

